# _*_coding:utf-8_*_
from ATS.instrumentDriver import InstGen

OK = 0
debug_mode = False


class DSO_2000(InstGen.SMU):
    def __init__(self, name, info_dict):
        InstGen.SMU.__init__(self, name, info_dict)
        self.rangei_list = [0, 1e-6, 10e-6, 100e-6, 1e-3, 20e-3, 1]
        self.rangev_list = [0, 0.2, 2, 20, 1000]
        print(name, info_dict)

    def __universalcom(self, cmdlist, func_id=0):
        """
        func_id: 0="write",1="read",2="ask",3="ask for values"
        """
        rval = None
        if not debug_mode:
            for cmd_str in cmdlist:
                if func_id == 0:
                    self.handler.write(cmd_str)
                elif func_id == 1:
                    rval = self.handler.read()
                elif func_id == 2:
                    rval = self.handler.ask(cmd_str)
                elif func_id == 3:
                    rval = self.handler.ask_for_values(cmd_str)
                elif func_id == 4:
                    rval = self.handler.read_values()
        else:
            for cmd in cmdlist:
                print("\n ", self.name, cmd)
                if func_id == 0:
                    self.handler.write(cmd)
                    if "?" not in cmd:
                        err = self.handler.ask(":syst:err?")
                        print("###error: ", err)
                elif func_id == 1:
                    rval = self.handler.read()
                elif func_id == 2:
                    rval = self.handler.ask(cmd)
                elif func_id == 3:
                    rval = self.handler.ask_for_values(cmd)
                elif func_id == 4:
                    rval = self.handler.read_values()
                print("\t", rval)
        return rval


def srcmeas(source):
    result = {source: {"Vpp": [], "RiseTime": [], "FallTime": []}}
    rval = universalcom({s: ["meas:vpp?"]}, 3)
    result[source]["Vpp"].append(rval[0])

    rval = universalcom({s: ["meas:ris?"]}, 3)
    result[source]["RiseTime"].append(rval[0])

    rval = universalcom({s: ["meas:fall?"]}, 3)
    result[source]["FallTime"].append(rval[0])


def universalcom(cmd_dict, func_id=0):
    """
    func_id: 0="write",1="read",2="ask",3="ask for values"
    """
    rval = None
    if not debug_mode:
        for s in cmd_dict:
            cmd_str = ";".join(cmd_dict[s])
            for cmd_str in cmd_dict[s]:
                if func_id == 0:
                    inst.write(s, cmd_str)
                elif func_id == 1:
                    rval = inst.read(s)
                elif func_id == 2:
                    rval = inst.ask(s, cmd_str)
                elif func_id == 3:
                    rval = inst.ask_for_values(s, cmd_str)
                elif func_id == 4:
                    rval = inst.read_values(s)
            cmd_dict[s] = []  # clear cmd dict
    else:
        for s in cmd_dict:
            for cmd in cmd_dict[s]:
                print("\n ", s, cmd)
                if func_id == 0:
                    inst.write(s, cmd)
                    if "?" not in cmd:
                        err = inst.ask(s, ":syst:err:all?")
                        print("error: ", err)
                elif func_id == 1:
                    rval = inst.read(s)
                elif func_id == 2:
                    rval = inst.ask(s, cmd)
                elif func_id == 3:
                    rval = inst.ask_for_values(s, cmd)
                elif func_id == 4:
                    rval = inst.read_values(s)
                print("\t", rval)
            cmd_dict[s] = []  # clear cmd dict
    print('rval is', rval)
    return rval
